package edu.ccut.saturn.studio.core.util.eclipse;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ide.IDE;

import edu.ccut.saturn.studio.core.saturnproperty.StringUtils;

/**
 * 针对选择对像的一些工具方法
 * 些方类中的方法可以跨平台
 * @author 郭英杰
 * @version 1.0 2008.9.4
 * 
 */
public final class SaturnEclipseSelectionUtil {
	
	/**
	 * @author 郭英杰
	 * @version 1.0 2008.9.4
	 * 
	 * 通过选择对像获得saturn平台特有KEY值
	 * @param selection
	 * @return
	 */
	public final static String selectionToSaturnKey(IStructuredSelection selection){
		List<String> srcs = new ArrayList<String>();
		String key = getProjectPath(selection);
		srcs = SaturnEclipseClassPathUtil.getClassPahtInfo(getSelectionToProject(selection), SaturnEclipseClassPathUtil.CLASSPATH_ATTRIBUTE_SRC);
		for (String string : srcs) {
			if(key.contains(string)){
				String src = StringUtils.DIRECOTRY_SEPARATOR+string;
				return key.replace(src, "").replace(StringUtils.DIRECOTRY_SEPARATOR, StringUtils.SATURN_FILE_SEPARATOR);
			}
		}
		return "";
	}
	
	/**
	 * @author 郭英杰
	 * @version 1.0 2008.9.4
	 * 
	 * 把正常的选择对像，转换成可以在ECLIPSE计算出来的对像
	 * 有于解决插件打包之后选择对像为NULL的场景
	 * @param selection
	 * @return
	 */
	public final static IStructuredSelection getComputeSelectedResourcesSelection(IStructuredSelection selection){
		IStructuredSelection structureSele = new StructuredSelection(IDE.computeSelectedResources(selection));
		selection = null;
		return structureSele;
	}
	
	/**
	 * @author 郭英杰
	 * @version 1.0 2008.9.4
	 * 
	 * 通过选择对像获得以工程为准的路径
	 * @param selection
	 * @return
	 */
	public final static String getProjectPath(IStructuredSelection selection){
		String fullPath = getFullPath(selection);
		String workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
		return fullPath.replace(workspacePath, "");
	}
	
	/**
	 * @author 郭英杰
	 * @version 1.0 2008.9.4
	 * 
	 * 通过选择对象获得工程名
	 * @param selection
	 * @return
	 */
	public final static String getProjectName(IStructuredSelection selection){
		StringBuffer projectPathPart1 = new StringBuffer(getProjectPath(selection));
		int startPart1 = projectPathPart1.indexOf(File.separator);
		String projectPathPart2 = projectPathPart1.substring(startPart1+1);
		int startPart2 = projectPathPart2.indexOf(File.separator);
		if(startPart2!=-1){
			return projectPathPart2.substring(0,startPart2);
		}
		return projectPathPart2;
	}
	
	/**
	 * @author 郭英杰
	 * @version 1.0 2008.9.4
	 * 
	 * 通过选择对象获得IProject对象
	 * @param selection
	 * @return
	 */
	public final static IProject getSelectionToProject(IStructuredSelection selection){
		String projectName = getProjectName(selection);
		return ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
	}
	
	/**
	 * @author 郭英杰
	 * @version 1.0 2008.9.4
	 * 
	 * 通过选择对象获得选择的全路径
	 * @param selection
	 * @return
	 */
	public final static String getFullPath(IStructuredSelection selection){
		IStructuredSelection structureSele = getComputeSelectedResourcesSelection(selection);
		String path = ((IResource) (structureSele.getFirstElement())).getLocation().toOSString();
		StringBuffer pahtBuffer = new StringBuffer(path);
		final String fullPath = pahtBuffer.append("").toString();
		structureSele = null;
		path = null;
		pahtBuffer = null;
		selection = null;
		return fullPath;
	}
}
